i = 0
nn.results <- list()

mod.formula <- formula(paste(dv,
                             paste(rhs,
                                   collapse = '+'),
                             sep = '~'))
for (year in start.year:end.year) {
  i = i + 1
  print(year)
  # Fit NN
  nn.results[[i]] <- list()
  dta.past <- dta[dta[,t.var]< year,]
  N <- length(dta.past[,dv])
  hours <- floor((proc.time()[3]-start.time)/3600)
  mins <- floor((proc.time()[3]-start.time)/60) - hours*60
  secs <- floor((proc.time()[3]-start.time)) - hours*3600 - mins*60
  
  print(paste(hours,
              "h",
              mins,
              "m",
              secs,
              "s elapsed",
              sep = ""))
 
  set.seed(i)
  if (i==1) {
    mod <- neuralnet(formula = mod.formula,
                     data = dta.past,
                     hidden = c(10,5),
                     threshold = 1,
                     stepmax=1e5)
  } else {
    mod <- neuralnet(formula = mod.formula,
                     data = dta.past,
                     hidden = c(10,5),
                     startweights = modlast$weights,
                     threshold = 1,
                     stepmax=1e5)
  }
  modlast = mod
  nn.results[[i]]$fit.oos <- predict(mod,
                                     newdata = dta[dta[,t.var]== year,rhs])
  nn.results[[i]]$actual.oos <- as.matrix(dta[dta[,t.var] == year,
                                              dv])
  print(year)
}
save(nn.results,file=paste(modeldir,"/",
                              country,
                              "_nn4_",
                              v,
                              "_",
                              rhs.group,
                              ".RData",
                              sep = ""))
